{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "@tf.function\n",
    "def printbar():\n",
    "    ts = tf.timestamp()\n",
    "    today_ts = ts % (24* 60*60)\n",
    "\n",
    "    hour = tf.cast(today_ts // 3600 + 8,tf.int32) % tf.constant(24)\n",
    "    minite = tf.cast((today_ts % 3600) // 60,tf.int32)\n",
    "    second = tf.cast(tf.floor(today_ts % 60),tf.int32)\n",
    "\n",
    "    def timeformat(m):\n",
    "        if tf.strings.length(tf.strings.format('{}',m)) == 1:\n",
    "            return tf.strings.format(\"0{}\",m)\n",
    "        else:\n",
    "            return tf.strings.format(\"{}\",m)\n",
    "\n",
    "    timestring = tf.strings.join(\n",
    "        [timeformat(hour),timeformat(minite),\n",
    "         timeformat(second)],separator=\":\"\n",
    "    )\n",
    "    tf.print(\"==========\" * 8,end=\"\")\n",
    "    tf.print(timestring)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "n =400\n",
    "X = tf.random.uniform([n,2],minval=-10,maxval=10)\n",
    "w0 = tf.constant([[2.0],[-1.0]])\n",
    "b0 = tf.constant(3.0)\n",
    "Y = X@w0 +b0 + tf.random.normal([n,1],mean=0.0,stddev=2.0)\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "================================================================================23:46:44\r\n",
      "epoch = 1000  loss = 2.46037698\r\n",
      "w = [[2.00923824]\n",
      " [-0.992889464]]\r\n",
      "b = 1.98198652\r\n",
      "\r\n",
      "================================================================================23:46:46\r\n",
      "epoch = 2000  loss = 1.90623713\r\n",
      "w = [[1.99964333]\n",
      " [-0.99899]]\r\n",
      "b = 2.69743824\r\n",
      "\r\n",
      "================================================================================23:46:48\r\n",
      "epoch = 3000  loss = 1.83003283\r\n",
      "w = [[1.99608481]\n",
      " [-1.00125134]]\r\n",
      "b = 2.96275139\r\n",
      "\r\n",
      "================================================================================23:46:50\r\n",
      "epoch = 4000  loss = 1.8195529\r\n",
      "w = [[1.99476528]\n",
      " [-1.0020901]]\r\n",
      "b = 3.06113982\r\n",
      "\r\n",
      "================================================================================23:46:52\r\n",
      "epoch = 5000  loss = 1.81811202\r\n",
      "w = [[1.99427629]\n",
      " [-1.00240266]]\r\n",
      "b = 3.09762549\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "w = tf.Variable(tf.random.normal(w0.shape))\n",
    "b = tf.Variable(0.0)\n",
    "\n",
    "def train(epoches):\n",
    "    for epoch in tf.range(1,epoches + 1):\n",
    "        with tf.GradientTape() as tape:\n",
    "            Y_hat = X@w + b\n",
    "            loss = tf.squeeze(tf.transpose(Y - Y_hat)@(Y - Y_hat)) / (2.0 *n)\n",
    "\n",
    "        dloss_dw,dloss_db = tape.gradient(loss,[w,b])\n",
    "\n",
    "        w.assign(w - 0.001 * dloss_dw)\n",
    "        b.assign(b - 0.001 * dloss_db)\n",
    "        if epoch % 1000 == 0:\n",
    "            printbar()\n",
    "            tf.print(\"epoch =\",epoch,' loss =',loss)\n",
    "            tf.print('w =',w)\n",
    "            tf.print('b =',b)\n",
    "            tf.print(\"\")\n",
    "\n",
    "train(5000)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "================================================================================23:51:27\r\n",
      "epoch = 1000  loss = 2.46763563\r\n",
      "w = [[2.00932431]\n",
      " [-0.992834806]]\r\n",
      "b = 1.97558129\r\n",
      "\r\n",
      "================================================================================23:51:27\r\n",
      "epoch = 2000  loss = 1.90723479\r\n",
      "w = [[1.99967504]\n",
      " [-0.998969793]]\r\n",
      "b = 2.69506335\r\n",
      "\r\n",
      "================================================================================23:51:27\r\n",
      "epoch = 3000  loss = 1.83016992\r\n",
      "w = [[1.99609661]\n",
      " [-1.00124395]]\r\n",
      "b = 2.96187186\r\n",
      "\r\n",
      "================================================================================23:51:27\r\n",
      "epoch = 4000  loss = 1.81957197\r\n",
      "w = [[1.99476957]\n",
      " [-1.00208735]]\r\n",
      "b = 3.06081295\r\n",
      "\r\n",
      "================================================================================23:51:27\r\n",
      "epoch = 5000  loss = 1.8181144\r\n",
      "w = [[1.99427783]\n",
      " [-1.00240159]]\r\n",
      "b = 3.09750533\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "w = tf.Variable(tf.random.normal(w0.shape))\n",
    "b = tf.Variable(0.0)\n",
    "\n",
    "@tf.function\n",
    "def train(epoches):\n",
    "    for epoch in tf.range(1,epoches + 1):\n",
    "        with tf.GradientTape() as tape:\n",
    "            Y_hat = X@w + b\n",
    "            loss = tf.squeeze(tf.transpose(Y - Y_hat)@(Y - Y_hat)) / ( 2.0 * n)\n",
    "        dloss_dw,dloss_db = tape.gradient(loss,[w,b])\n",
    "        w.assign(w - 0.001 * dloss_dw)\n",
    "        b.assign(b - 0.001 * dloss_db)\n",
    "        if epoch % 1000 == 0:\n",
    "            printbar()\n",
    "            tf.print(\"epoch =\",epoch,' loss =',loss)\n",
    "            tf.print('w =',w)\n",
    "            tf.print('b =',b)\n",
    "            tf.print(\"\")\n",
    "train(5000)\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}